<?php
// $Id: to_do_action.module,v 1.1.2.1 2010/05/23 12:11:09 alexiswilke Exp $

function to_do_action_hook_info() {
  return array(
    'to_do_action' => array(
      'to_do_action' => array(
        'to_do_started' => array(
          'runs when' => t('A user clicked on the Started button.'),
        ),
        'to_do_reminder' => array(
          'runs when' => t('One of your reminder date and time was reached.'),
        ),
        'to_do_deadline_reached' => array(
          'runs when' => t('Today the deadline was reached.'),
        ),
        'to_do_past_deadline' => array(
          'runs when' => t('You are late. The deadline was not met.'),
        ),
        'to_do_finished' => array(
          'runs when' => t('The To do item was marked as finished.'),
        ),
        'to_do_unpublished' => array(
          'runs when' => t('The To do item was unpublished.'),
        ),
        'to_do_deleting' => array(
          'runs when' => t('The To do item is about to be deleted.'),
        ),
      ),
    ),
  );
}

function to_do_action_action_info_alter(&$actions) {
  $callbacks = array(
    'to_do_started',
    'to_do_reminder',
    'to_do_deadline_reached',
    'to_do_past_deadline',
    'to_do_finished',
    'to_do_unpublished',
    'to_do_deleting',
  );

  $actions['node_publish_action']['hooks']['to_do_action'] = $callbacks;
  $actions['node_unpublish_action']['hooks']['to_do_action'] = $callbacks;
  $actions['node_make_sticky_action']['hooks']['to_do_action'] = $callbacks;
  $actions['node_make_unsticky_action']['hooks']['to_do_action'] = $callbacks;
  $actions['node_promote_action']['hooks']['to_do_action'] = $callbacks;
  $actions['node_unpromote_action']['hooks']['to_do_action'] = $callbacks;
  $actions['node_assign_owner_action']['hooks']['to_do_action'] = $callbacks;

  $actions['system_message_action']['hooks']['to_do_action'] = $callbacks;
  $actions['system_send_email_action']['hooks']['to_do_action'] = $callbacks;
  $actions['system_goto_action']['hooks']['to_do_action'] = $callbacks;
}

function to_do_action_trigger($op, $node) {
  global $user;

  $aids = _trigger_get_hook_aids('to_do_action', $op);
  if (count($aids) == 0) {
    return;
  }

  $author = user_load(array('uid' => $node->uid));
  if (!$author) {
    // in case it was deleted and not correctly defined in the node
    $author = $user;
  }

  $context = array(
    'hook' => 'to_do_action',
    'op' => $op,
    'node' => $node,
    'author' => $author,
    'author_email' => $author->mail,
    // add assigned users?
  );

  foreach ($aids as $aid => $action_info) {
    actions_do($aid, $node, $context);
  }
}

function to_do_action_to_do_started($op, $node) {
  to_do_action_trigger('to_do_started', $node);
}

function to_do_action_to_do_reminder($op, $node) {
  to_do_action_trigger('to_do_reminder', $node);
}

function to_do_action_to_do_deadline_reached($op, $node) {
  to_do_action_trigger('to_do_deadline_reached', $node);
}

function to_do_action_to_do_past_deadline($op, $node) {
  to_do_action_trigger('to_do_past_deadline', $node);
}

function to_do_action_to_do_finished($op, $node) {
  to_do_action_trigger('to_do_finished', $node);
}

function to_do_action_to_do_unpublished($op, $node) {
  to_do_action_trigger('to_do_unpublished', $node);
}

function to_do_action_to_do_deleting($op, $node) {
  to_do_action_trigger('to_do_deleting', $node);
}

// vim: ts=2 sw=2 et syntax=php
